{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b91b982c-5e48-4f88-bd69-e1b7129b2ef2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:44.385316Z",
     "iopub.status.busy": "2022-04-07T01:51:44.384893Z",
     "iopub.status.idle": "2022-04-07T01:51:47.998308Z",
     "shell.execute_reply": "2022-04-07T01:51:47.997710Z",
     "shell.execute_reply.started": "2022-04-07T01:51:44.385217Z"
    }
   },
   "outputs": [],
   "source": [
    "# 引入Numpy包\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fb78234f-b236-417b-a195-a62041d9ecbe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.000456Z",
     "iopub.status.busy": "2022-04-07T01:51:48.000251Z",
     "iopub.status.idle": "2022-04-07T01:51:48.010920Z",
     "shell.execute_reply": "2022-04-07T01:51:48.010302Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.000433Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -2.31184687,   2.92967497,  -1.69130567,  15.73001027,\n",
       "         14.53253428],\n",
       "       [ -7.15998078, -15.41770466,  19.87120689,  13.62094683,\n",
       "         16.52028935],\n",
       "       [  6.50019617, -14.94476839, -10.89458539,  17.40596821,\n",
       "         19.85411989],\n",
       "       [  5.63571719, -16.23753472,  11.6423797 ,  -2.72099872,\n",
       "          0.70716693]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.uniform(-20, 20, size=(4, 5))\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f05ee17-0510-4296-9922-c742ced98255",
   "metadata": {},
   "source": [
    "## 一元函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3e597855-4007-4a51-a23b-44e276eef2f0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.012284Z",
     "iopub.status.busy": "2022-04-07T01:51:48.011858Z",
     "iopub.status.idle": "2022-04-07T01:51:48.017146Z",
     "shell.execute_reply": "2022-04-07T01:51:48.016555Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.012258Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.31184687,  2.92967497,  1.69130567, 15.73001027, 14.53253428],\n",
       "       [ 7.15998078, 15.41770466, 19.87120689, 13.62094683, 16.52028935],\n",
       "       [ 6.50019617, 14.94476839, 10.89458539, 17.40596821, 19.85411989],\n",
       "       [ 5.63571719, 16.23753472, 11.6423797 ,  2.72099872,  0.70716693]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.abs：求绝对值\n",
    "np.abs(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3bef3ee8-f772-42cc-91ad-35a6c26bb8a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.019396Z",
     "iopub.status.busy": "2022-04-07T01:51:48.018622Z",
     "iopub.status.idle": "2022-04-07T01:51:48.026540Z",
     "shell.execute_reply": "2022-04-07T01:51:48.025864Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.019352Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_15437/231563697.py:2: RuntimeWarning: invalid value encountered in sqrt\n",
      "  np.sqrt(data)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[       nan, 1.71162933,        nan, 3.9661077 , 3.81215612],\n",
       "       [       nan,        nan, 4.45771319, 3.69065669, 4.06451588],\n",
       "       [2.54954823,        nan,        nan, 4.17204605, 4.45579621],\n",
       "       [2.37396655,        nan, 3.41209315,        nan, 0.84093218]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.sqrt：开平方根\n",
    "np.sqrt(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "705bbd59-ef8a-4504-a7d4-e3272b71aaea",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.028237Z",
     "iopub.status.busy": "2022-04-07T01:51:48.027926Z",
     "iopub.status.idle": "2022-04-07T01:51:48.033000Z",
     "shell.execute_reply": "2022-04-07T01:51:48.032378Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.028212Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  5.34463594,   8.58299542,   2.86051487, 247.43322316,\n",
       "        211.19455258],\n",
       "       [ 51.26532481, 237.70561687, 394.86486344, 185.53019246,\n",
       "        272.91996023],\n",
       "       [ 42.25255026, 223.34610215, 118.69199087, 302.96772943,\n",
       "        394.18607677],\n",
       "       [ 31.76130827, 263.6575339 , 135.54500502,   7.40383403,\n",
       "          0.50008506]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.square：求平方\n",
    "np.square(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "22839e32-ec31-4b89-851a-9c291886a1c1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.034441Z",
     "iopub.status.busy": "2022-04-07T01:51:48.033919Z",
     "iopub.status.idle": "2022-04-07T01:51:48.040454Z",
     "shell.execute_reply": "2022-04-07T01:51:48.039436Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.034416Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9.90780982e-02, 1.87215444e+01, 1.84278760e-01, 6.78354424e+06,\n",
       "        2.04832774e+06],\n",
       "       [7.77069486e-04, 2.01454023e-07, 4.26535815e+08, 8.23193714e+05,\n",
       "        1.49510091e+07],\n",
       "       [6.65272127e+02, 3.23273091e-07, 1.85584492e-05, 3.62506624e+07,\n",
       "        4.19309511e+08],\n",
       "       [2.80259845e+02, 8.87418074e-08, 1.13820702e+05, 6.58089969e-02,\n",
       "        2.02823697e+00]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.exp：求指数\n",
    "np.exp(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f21c8bbe-baad-43e4-8c85-09361ea2249a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.045186Z",
     "iopub.status.busy": "2022-04-07T01:51:48.044729Z",
     "iopub.status.idle": "2022-04-07T01:51:48.050255Z",
     "shell.execute_reply": "2022-04-07T01:51:48.049550Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.045162Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/59/_lbpck214pqcv3v468n7dv6w0000gn/T/ipykernel_15437/1771881011.py:2: RuntimeWarning: invalid value encountered in log\n",
      "  np.log(data)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[        nan,  1.07489148,         nan,  2.75557037,  2.67638988],\n",
       "       [        nan,         nan,  2.98927179,  2.61160882,  2.80458928],\n",
       "       [ 1.87183236,         nan,         nan,  2.85681315,  2.98841154],\n",
       "       [ 1.72912441,         nan,  2.45465186,         nan, -0.34648854]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.log：求对数\n",
    "np.log(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5569f2a1-dee5-41eb-834d-093f71c732c5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.051718Z",
     "iopub.status.busy": "2022-04-07T01:51:48.051378Z",
     "iopub.status.idle": "2022-04-07T01:51:48.058581Z",
     "shell.execute_reply": "2022-04-07T01:51:48.057719Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.051648Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.,  1., -1.,  1.,  1.],\n",
       "       [-1., -1.,  1.,  1.,  1.],\n",
       "       [ 1., -1., -1.,  1.,  1.],\n",
       "       [ 1., -1.,  1., -1.,  1.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.sign：数值标签化（大于0变成1，小于0变成-1）\n",
    "np.sign(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "755b4a3f-2f3a-42d7-945c-20787085c1be",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.060268Z",
     "iopub.status.busy": "2022-04-07T01:51:48.059951Z",
     "iopub.status.idle": "2022-04-07T01:51:48.065561Z",
     "shell.execute_reply": "2022-04-07T01:51:48.064889Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.060241Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -2.,   3.,  -1.,  16.,  15.],\n",
       "       [ -7., -15.,  20.,  14.,  17.],\n",
       "       [  7., -14., -10.,  18.,  20.],\n",
       "       [  6., -16.,  12.,  -2.,   1.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.ceil：向上取整\n",
    "np.ceil(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a8cb528b-acb4-40ec-81b5-8a843e4bcbdd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.066875Z",
     "iopub.status.busy": "2022-04-07T01:51:48.066497Z",
     "iopub.status.idle": "2022-04-07T01:51:48.072855Z",
     "shell.execute_reply": "2022-04-07T01:51:48.071850Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.066851Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -3.,   2.,  -2.,  15.,  14.],\n",
       "       [ -8., -16.,  19.,  13.,  16.],\n",
       "       [  6., -15., -11.,  17.,  19.],\n",
       "       [  5., -17.,  11.,  -3.,   0.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.floor：向下取整\n",
    "np.floor(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dd777456-27a3-441e-b700-74feec19efec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.074754Z",
     "iopub.status.busy": "2022-04-07T01:51:48.074371Z",
     "iopub.status.idle": "2022-04-07T01:51:48.080047Z",
     "shell.execute_reply": "2022-04-07T01:51:48.079307Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.074727Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -2.,   3.,  -2.,  16.,  15.],\n",
       "       [ -7., -15.,  20.,  14.,  17.],\n",
       "       [  7., -15., -11.,  17.,  20.],\n",
       "       [  6., -16.,  12.,  -3.,   1.]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.rint或者np.round：四舍五入\n",
    "np.round(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "52d7a285-e4aa-44e4-bc7e-2eb4626f9a31",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.081543Z",
     "iopub.status.busy": "2022-04-07T01:51:48.081148Z",
     "iopub.status.idle": "2022-04-07T01:51:48.087673Z",
     "shell.execute_reply": "2022-04-07T01:51:48.086610Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.081518Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[-0.31184687,  0.92967497, -0.69130567,  0.73001027,  0.53253428],\n",
       "        [-0.15998078, -0.41770466,  0.87120689,  0.62094683,  0.52028935],\n",
       "        [ 0.50019617, -0.94476839, -0.89458539,  0.40596821,  0.85411989],\n",
       "        [ 0.63571719, -0.23753472,  0.6423797 , -0.72099872,  0.70716693]]),\n",
       " array([[ -2.,   2.,  -1.,  15.,  14.],\n",
       "        [ -7., -15.,  19.,  13.,  16.],\n",
       "        [  6., -14., -10.,  17.,  19.],\n",
       "        [  5., -16.,  11.,  -2.,   0.]]))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.modf：整数部分和小数部分分离\n",
    "np.modf(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13d960b3-594d-44a3-a94a-baf406607b9e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:25:26.931385Z",
     "iopub.status.busy": "2022-04-07T03:25:26.931135Z",
     "iopub.status.idle": "2022-04-07T03:25:26.934785Z",
     "shell.execute_reply": "2022-04-07T03:25:26.934040Z",
     "shell.execute_reply.started": "2022-04-07T03:25:26.931359Z"
    },
    "tags": []
   },
   "source": [
    "## 二元函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8cdd0f56-e8ac-4f14-90ac-31c572b9d084",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:51:48.089787Z",
     "iopub.status.busy": "2022-04-07T01:51:48.089476Z",
     "iopub.status.idle": "2022-04-07T01:51:48.096005Z",
     "shell.execute_reply": "2022-04-07T01:51:48.095187Z",
     "shell.execute_reply.started": "2022-04-07T01:51:48.089756Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -1.31184687,   3.92967497,  -0.69130567,  16.73001027,\n",
       "         15.53253428],\n",
       "       [ -6.15998078, -14.41770466,  20.87120689,  14.62094683,\n",
       "         17.52028935],\n",
       "       [  7.50019617, -13.94476839,  -9.89458539,  18.40596821,\n",
       "         20.85411989],\n",
       "       [  6.63571719, -15.23753472,  12.6423797 ,  -1.72099872,\n",
       "          1.70716693]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加法操作np.add或者运算符+\n",
    "np.add(data, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "667da83d-3ab8-4c4a-a450-2d8def34495e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T01:58:28.941051Z",
     "iopub.status.busy": "2022-04-07T01:58:28.940778Z",
     "iopub.status.idle": "2022-04-07T01:58:28.946654Z",
     "shell.execute_reply": "2022-04-07T01:58:28.946035Z",
     "shell.execute_reply.started": "2022-04-07T01:58:28.941022Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -2.31184687,   4.92967497,   1.30869433,  17.73001027,\n",
       "         14.53253428],\n",
       "       [ -6.15998078, -14.41770466,  19.87120689,  14.62094683,\n",
       "         16.52028935],\n",
       "       [ 10.50019617, -10.94476839, -10.89458539,  17.40596821,\n",
       "         23.85411989],\n",
       "       [  7.63571719, -16.23753472,  12.6423797 ,   0.27900128,\n",
       "          4.70716693]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加法操作np.add或者运算符+\n",
    "np.add(data, np.random.randint(0, 5, size=(4, 5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "28dd92d3-534c-4b06-8173-536727d136af",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T02:16:13.486363Z",
     "iopub.status.busy": "2022-04-07T02:16:13.486127Z",
     "iopub.status.idle": "2022-04-07T02:16:13.491160Z",
     "shell.execute_reply": "2022-04-07T02:16:13.490563Z",
     "shell.execute_reply.started": "2022-04-07T02:16:13.486340Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.92967497, 15.73001027, 14.53253428, 19.87120689, 13.62094683,\n",
       "       16.52028935,  6.50019617, 17.40596821, 19.85411989,  5.63571719,\n",
       "       11.6423797 ,  0.70716693])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求data中大于0的数\n",
    "data[data > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bdcaf49c-649f-421f-86db-a97f5ea77cfd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T02:17:26.760850Z",
     "iopub.status.busy": "2022-04-07T02:17:26.760276Z",
     "iopub.status.idle": "2022-04-07T02:17:26.765800Z",
     "shell.execute_reply": "2022-04-07T02:17:26.765182Z",
     "shell.execute_reply.started": "2022-04-07T02:17:26.760823Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.92967497, 15.73001027, 14.53253428, 19.87120689, 13.62094683,\n",
       "       16.52028935,  6.50019617, 17.40596821, 19.85411989,  5.63571719,\n",
       "       11.6423797 ,  0.70716693])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求data中大于0的数\n",
    "data[np.greater(data, 0)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9de93f24-d07d-41b6-a8dc-6ba70cc3fc5c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T02:29:14.155397Z",
     "iopub.status.busy": "2022-04-07T02:29:14.155121Z",
     "iopub.status.idle": "2022-04-07T02:29:14.160581Z",
     "shell.execute_reply": "2022-04-07T02:29:14.159791Z",
     "shell.execute_reply.started": "2022-04-07T02:29:14.155369Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.92967497, 6.50019617, 5.63571719, 0.70716693])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求data中大于0，小于10的数\n",
    "data[(data > 0) & (data < 10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0876d7b2-691a-48a3-a0d6-3e52c4079860",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T02:30:42.694605Z",
     "iopub.status.busy": "2022-04-07T02:30:42.694033Z",
     "iopub.status.idle": "2022-04-07T02:30:42.699615Z",
     "shell.execute_reply": "2022-04-07T02:30:42.698989Z",
     "shell.execute_reply.started": "2022-04-07T02:30:42.694577Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.92967497, 6.50019617, 5.63571719, 0.70716693])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求data中大于0，小于10的数\n",
    "data[np.logical_and(data > 0, data < 10)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73518688-4d13-4249-908a-0ad12f6a6409",
   "metadata": {},
   "source": [
    "## 聚合函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8f14558d-9d8a-439f-8e33-181a884217ee",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:27:07.323718Z",
     "iopub.status.busy": "2022-04-07T03:27:07.323403Z",
     "iopub.status.idle": "2022-04-07T03:27:07.328629Z",
     "shell.execute_reply": "2022-04-07T03:27:07.327854Z",
     "shell.execute_reply.started": "2022-04-07T03:27:07.323688Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "73.57148548663861"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sum求和\n",
    "np.sum(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "034074ea-8911-4c5e-9f66-1d3aa38b03b5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:27:53.935687Z",
     "iopub.status.busy": "2022-04-07T03:27:53.935324Z",
     "iopub.status.idle": "2022-04-07T03:27:53.940684Z",
     "shell.execute_reply": "2022-04-07T03:27:53.940074Z",
     "shell.execute_reply.started": "2022-04-07T03:27:53.935659Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  2.66408571, -43.6703328 ,  18.92769553,  44.03592659,\n",
       "        51.61411045])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sum求和,列求和\n",
    "np.sum(data, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3baa5cd6-7d15-4234-b549-64eb8d3b56aa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:28:11.546565Z",
     "iopub.status.busy": "2022-04-07T03:28:11.546292Z",
     "iopub.status.idle": "2022-04-07T03:28:11.551687Z",
     "shell.execute_reply": "2022-04-07T03:28:11.551013Z",
     "shell.execute_reply.started": "2022-04-07T03:28:11.546531Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([29.18906698, 27.43475763, 17.9209305 , -0.97326963])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sum求和,行求和\n",
    "np.sum(data, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "aed10ce4-7134-46f0-b6db-2eaeb1eaccb3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:50:30.675473Z",
     "iopub.status.busy": "2022-04-07T03:50:30.675232Z",
     "iopub.status.idle": "2022-04-07T03:50:30.681057Z",
     "shell.execute_reply": "2022-04-07T03:50:30.679967Z",
     "shell.execute_reply.started": "2022-04-07T03:50:30.675449Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.691438632808781e+17"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# prod求积\n",
    "np.prod(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c82ed18e-16db-4bbe-9c77-0b1baa0a4b3a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:51:27.517634Z",
     "iopub.status.busy": "2022-04-07T03:51:27.517334Z",
     "iopub.status.idle": "2022-04-07T03:51:27.523114Z",
     "shell.execute_reply": "2022-04-07T03:51:27.522463Z",
     "shell.execute_reply.started": "2022-04-07T03:51:27.517606Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   606.38238352, -10960.95624248,   4262.83787928, -10147.58802049,\n",
       "         3370.78914745])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# prod求积,按列求积\n",
    "np.prod(data, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2ad218b1-12d8-4ca9-8e14-a59723bde674",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:51:42.176440Z",
     "iopub.status.busy": "2022-04-07T03:51:42.176189Z",
     "iopub.status.idle": "2022-04-07T03:51:42.181555Z",
     "shell.execute_reply": "2022-04-07T03:51:42.180931Z",
     "shell.execute_reply.started": "2022-04-07T03:51:42.176413Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  2618.61090161, 493606.38753325, 365742.29502638,   2050.03521686])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# prod求积,按行求积\n",
    "np.prod(data, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "35911062-48c2-4d79-b539-fa84db3099a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:55:31.218811Z",
     "iopub.status.busy": "2022-04-07T03:55:31.218519Z",
     "iopub.status.idle": "2022-04-07T03:55:31.223659Z",
     "shell.execute_reply": "2022-04-07T03:55:31.223018Z",
     "shell.execute_reply.started": "2022-04-07T03:55:31.218767Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.6785742743319303"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# mean求均值\n",
    "np.mean(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "00d8079c-142d-4e6d-af5d-746e2aa659c4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:57:42.890633Z",
     "iopub.status.busy": "2022-04-07T03:57:42.890368Z",
     "iopub.status.idle": "2022-04-07T03:57:42.896246Z",
     "shell.execute_reply": "2022-04-07T03:57:42.895229Z",
     "shell.execute_reply.started": "2022-04-07T03:57:42.890603Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.66602143, -10.9175832 ,   4.73192388,  11.00898165,\n",
       "        12.90352761])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# mean求均值,按列求均值\n",
    "np.mean(data, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "501fe5f7-588d-4d8a-9bfb-f1cd8d6b52be",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T03:57:59.641999Z",
     "iopub.status.busy": "2022-04-07T03:57:59.641736Z",
     "iopub.status.idle": "2022-04-07T03:57:59.646957Z",
     "shell.execute_reply": "2022-04-07T03:57:59.646262Z",
     "shell.execute_reply.started": "2022-04-07T03:57:59.641973Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5.8378134 ,  5.48695153,  3.5841861 , -0.19465393])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# mean求均值,按行求均值\n",
    "np.mean(data, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "fc26c79f-1b1b-4c44-860e-fae871577077",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T07:12:42.314485Z",
     "iopub.status.busy": "2022-04-07T07:12:42.314188Z",
     "iopub.status.idle": "2022-04-07T07:12:42.319712Z",
     "shell.execute_reply": "2022-04-07T07:12:42.318978Z",
     "shell.execute_reply.started": "2022-04-07T07:12:42.314458Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11.973670952816722"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# std求标准差\n",
    "np.std(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "13d85cdf-cbd7-4708-b01b-a4ce907a7d27",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T07:13:23.052851Z",
     "iopub.status.busy": "2022-04-07T07:13:23.052569Z",
     "iopub.status.idle": "2022-04-07T07:13:23.057921Z",
     "shell.execute_reply": "2022-04-07T07:13:23.057249Z",
     "shell.execute_reply.started": "2022-04-07T07:13:23.052823Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5.67559427,  8.00808586, 11.85746558,  8.03965595,  7.29377432])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# std求标准差,按列求标准差\n",
    "np.std(data, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "55420f14-1898-427e-94ad-61aa2d200659",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T07:13:41.760971Z",
     "iopub.status.busy": "2022-04-07T07:13:41.760651Z",
     "iopub.status.idle": "2022-04-07T07:13:41.766566Z",
     "shell.execute_reply": "2022-04-07T07:13:41.765838Z",
     "shell.execute_reply.started": "2022-04-07T07:13:41.760941Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7.81044936, 14.08369818, 14.2633271 ,  9.36673172])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# std求标准差,按行求标准差\n",
    "np.std(data, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "75f7eb2c-e0cf-4f1a-bd6c-239dd3d96a90",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T07:24:00.228120Z",
     "iopub.status.busy": "2022-04-07T07:24:00.227875Z",
     "iopub.status.idle": "2022-04-07T07:24:00.232646Z",
     "shell.execute_reply": "2022-04-07T07:24:00.232000Z",
     "shell.execute_reply.started": "2022-04-07T07:24:00.228093Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ -2.31184687   2.92967497  -1.69130567  15.73001027  14.53253428]\n",
      " [ -7.15998078 -15.41770466  19.87120689  13.62094683  16.52028935]\n",
      " [  6.50019617 -14.94476839 -10.89458539  17.40596821  19.85411989]\n",
      " [  5.63571719 -16.23753472  11.6423797   -2.72099872   0.70716693]]\n",
      "-16.237534723573646\n",
      "16\n"
     ]
    }
   ],
   "source": [
    "# argmin求最小值索引\n",
    "print(data)\n",
    "print(np.min(data))\n",
    "print(np.argmin(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1c1cd22c-7991-4bbd-bea2-2e8f17c90247",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T07:31:25.595419Z",
     "iopub.status.busy": "2022-04-07T07:31:25.595169Z",
     "iopub.status.idle": "2022-04-07T07:31:25.600890Z",
     "shell.execute_reply": "2022-04-07T07:31:25.599962Z",
     "shell.execute_reply.started": "2022-04-07T07:31:25.595392Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ -2.31184687   2.92967497  -1.69130567  15.73001027  14.53253428]\n",
      " [ -7.15998078 -15.41770466  19.87120689  13.62094683  16.52028935]\n",
      " [  6.50019617 -14.94476839 -10.89458539  17.40596821  19.85411989]\n",
      " [  5.63571719 -16.23753472  11.6423797   -2.72099872   0.70716693]]\n",
      "[ -7.15998078 -16.23753472 -10.89458539  -2.72099872   0.70716693]\n",
      "[1 3 2 3 3]\n"
     ]
    }
   ],
   "source": [
    "# argmin求最小值索引,按列求\n",
    "print(data)\n",
    "print(np.min(data, axis=0))\n",
    "print(np.argmin(data, axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c8ebf4d6-0dba-48b9-9ad0-b3f63a7fb58a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T08:23:24.706983Z",
     "iopub.status.busy": "2022-04-07T08:23:24.706730Z",
     "iopub.status.idle": "2022-04-07T08:23:24.803485Z",
     "shell.execute_reply": "2022-04-07T08:23:24.802959Z",
     "shell.execute_reply.started": "2022-04-07T08:23:24.706957Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "499999500000"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(np.arange(1000000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "25d056ac-0642-4f7d-bed3-07a6f4bc11dc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T08:23:46.574617Z",
     "iopub.status.busy": "2022-04-07T08:23:46.574367Z",
     "iopub.status.idle": "2022-04-07T08:23:46.581485Z",
     "shell.execute_reply": "2022-04-07T08:23:46.580836Z",
     "shell.execute_reply.started": "2022-04-07T08:23:46.574590Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "499999500000"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(np.arange(1000000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c8d7753-6f1c-4eb5-af2a-1798233790cf",
   "metadata": {},
   "source": [
    "## 布尔函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5ccb77a8-e957-4811-baa4-ba6a98f39d33",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T09:00:22.480955Z",
     "iopub.status.busy": "2022-04-07T09:00:22.480622Z",
     "iopub.status.idle": "2022-04-07T09:00:22.485629Z",
     "shell.execute_reply": "2022-04-07T09:00:22.484833Z",
     "shell.execute_reply.started": "2022-04-07T09:00:22.480931Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不给条件，any判断是否有元素不等于0\n",
    "np.any(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "8be702c9-2947-43fe-b67a-2777eecc5d55",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T09:00:44.982644Z",
     "iopub.status.busy": "2022-04-07T09:00:44.982134Z",
     "iopub.status.idle": "2022-04-07T09:00:44.987682Z",
     "shell.execute_reply": "2022-04-07T09:00:44.986972Z",
     "shell.execute_reply.started": "2022-04-07T09:00:44.982614Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不给条件，all判断是否所有元素不等于0\n",
    "np.all(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "0bbea1d7-f57b-4871-8005-2ce73718a92e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T09:13:22.094300Z",
     "iopub.status.busy": "2022-04-07T09:13:22.094042Z",
     "iopub.status.idle": "2022-04-07T09:13:22.099453Z",
     "shell.execute_reply": "2022-04-07T09:13:22.098709Z",
     "shell.execute_reply.started": "2022-04-07T09:13:22.094273Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(np.array([0, 0, 0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "a4750c7f-7311-4471-825b-b3482d09037e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T08:54:47.223035Z",
     "iopub.status.busy": "2022-04-07T08:54:47.222794Z",
     "iopub.status.idle": "2022-04-07T08:54:47.227674Z",
     "shell.execute_reply": "2022-04-07T08:54:47.227040Z",
     "shell.execute_reply.started": "2022-04-07T08:54:47.223012Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给条件判断，any判断是否有元素都满足条件\n",
    "np.any(data > 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ccf526af-8c22-4ac6-98c0-69b69c1f00c0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T08:54:36.497584Z",
     "iopub.status.busy": "2022-04-07T08:54:36.497329Z",
     "iopub.status.idle": "2022-04-07T08:54:36.502842Z",
     "shell.execute_reply": "2022-04-07T08:54:36.501870Z",
     "shell.execute_reply.started": "2022-04-07T08:54:36.497558Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给条件判断，all判断是否所有元素都满足条件\n",
    "np.all(data > 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "57539f06-3cc5-4165-b096-d3331f362d3a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T08:57:10.351372Z",
     "iopub.status.busy": "2022-04-07T08:57:10.351040Z",
     "iopub.status.idle": "2022-04-07T08:57:10.356197Z",
     "shell.execute_reply": "2022-04-07T08:57:10.355505Z",
     "shell.execute_reply.started": "2022-04-07T08:57:10.351343Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(data != 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c72cc822-66db-41e2-9950-1ab903201b0a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T09:28:11.902830Z",
     "iopub.status.busy": "2022-04-07T09:28:11.902425Z",
     "iopub.status.idle": "2022-04-07T09:28:11.907712Z",
     "shell.execute_reply": "2022-04-07T09:28:11.906962Z",
     "shell.execute_reply.started": "2022-04-07T09:28:11.902802Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给条件判断，all判断是否所有元素都满足条件\n",
    "(data > 0).all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "1297f7ba-b167-414f-b713-37e0934f4388",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T09:28:25.762837Z",
     "iopub.status.busy": "2022-04-07T09:28:25.762543Z",
     "iopub.status.idle": "2022-04-07T09:28:25.767946Z",
     "shell.execute_reply": "2022-04-07T09:28:25.767345Z",
     "shell.execute_reply.started": "2022-04-07T09:28:25.762810Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给条件判断，any判断是否有元素都满足条件\n",
    "(data > 0).any()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a8d4b4e-9b0f-44d0-a86a-16e6a367b7fe",
   "metadata": {},
   "source": [
    "## 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "1cc93f67-20d3-4fd2-8b00-933c080ad5f9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T09:53:48.072893Z",
     "iopub.status.busy": "2022-04-07T09:53:48.072640Z",
     "iopub.status.idle": "2022-04-07T09:53:48.077824Z",
     "shell.execute_reply": "2022-04-07T09:53:48.077145Z",
     "shell.execute_reply.started": "2022-04-07T09:53:48.072866Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ -2.31184687   2.92967497  -1.69130567  15.73001027  14.53253428]\n",
      " [ -7.15998078 -15.41770466  19.87120689  13.62094683  16.52028935]\n",
      " [  6.50019617 -14.94476839 -10.89458539  17.40596821  19.85411989]\n",
      " [  5.63571719 -16.23753472  11.6423797   -2.72099872   0.70716693]]\n",
      "======================================================================\n",
      "[[ -2.31184687  -1.69130567   2.92967497  14.53253428  15.73001027]\n",
      " [-15.41770466  -7.15998078  13.62094683  16.52028935  19.87120689]\n",
      " [-14.94476839 -10.89458539   6.50019617  17.40596821  19.85411989]\n",
      " [-16.23753472  -2.72099872   0.70716693   5.63571719  11.6423797 ]]\n"
     ]
    }
   ],
   "source": [
    "# 不指定axis进排序，默认最内层\n",
    "print(data)\n",
    "print(\"=\" * 70)\n",
    "print(np.sort(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "9afba06e-179d-428f-8fc1-5c8cd79312e0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T09:58:54.259664Z",
     "iopub.status.busy": "2022-04-07T09:58:54.259407Z",
     "iopub.status.idle": "2022-04-07T09:58:54.265134Z",
     "shell.execute_reply": "2022-04-07T09:58:54.264326Z",
     "shell.execute_reply.started": "2022-04-07T09:58:54.259636Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ -2.31184687   2.92967497  -1.69130567  15.73001027  14.53253428]\n",
      " [ -7.15998078 -15.41770466  19.87120689  13.62094683  16.52028935]\n",
      " [  6.50019617 -14.94476839 -10.89458539  17.40596821  19.85411989]\n",
      " [  5.63571719 -16.23753472  11.6423797   -2.72099872   0.70716693]]\n",
      "======================================================================\n",
      "[[ -7.15998078 -16.23753472 -10.89458539  -2.72099872   0.70716693]\n",
      " [ -2.31184687 -15.41770466  -1.69130567  13.62094683  14.53253428]\n",
      " [  5.63571719 -14.94476839  11.6423797   15.73001027  16.52028935]\n",
      " [  6.50019617   2.92967497  19.87120689  17.40596821  19.85411989]]\n"
     ]
    }
   ],
   "source": [
    "# 指定axis=1进排序\n",
    "print(data)\n",
    "print(\"=\" * 70)\n",
    "print(np.sort(data, axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "7075c40a-1ff1-4e33-a349-f8aff5a36338",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T10:12:47.452648Z",
     "iopub.status.busy": "2022-04-07T10:12:47.452398Z",
     "iopub.status.idle": "2022-04-07T10:12:47.458583Z",
     "shell.execute_reply": "2022-04-07T10:12:47.457726Z",
     "shell.execute_reply.started": "2022-04-07T10:12:47.452621Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ -2.31184687  -1.69130567   2.92967497  14.53253428  15.73001027]\n",
      " [-15.41770466  -7.15998078  13.62094683  16.52028935  19.87120689]\n",
      " [-14.94476839 -10.89458539   6.50019617  17.40596821  19.85411989]\n",
      " [-16.23753472  -2.72099872   0.70716693   5.63571719  11.6423797 ]]\n",
      "======================================================================\n",
      "[[-16.23753472 -10.89458539   0.70716693   5.63571719  11.6423797 ]\n",
      " [-15.41770466  -7.15998078   2.92967497  14.53253428  15.73001027]\n",
      " [-14.94476839  -2.72099872   6.50019617  16.52028935  19.85411989]\n",
      " [ -2.31184687  -1.69130567  13.62094683  17.40596821  19.87120689]]\n"
     ]
    }
   ],
   "source": [
    "# ndarray.sort进行排序\n",
    "print(data)\n",
    "data.sort(axis=0)\n",
    "print(\"=\" * 70)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "a67f470e-168f-453e-b1ae-c9fdc0067fc6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T10:30:41.209066Z",
     "iopub.status.busy": "2022-04-07T10:30:41.208823Z",
     "iopub.status.idle": "2022-04-07T10:30:41.214340Z",
     "shell.execute_reply": "2022-04-07T10:30:41.213500Z",
     "shell.execute_reply.started": "2022-04-07T10:30:41.209039Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-10.7030199    6.92446262  -4.30791428   8.44456621 -12.65383058]\n",
      " [ 15.89937381  15.24068584 -10.11422686 -14.3599904   11.00322691]\n",
      " [ 10.98773486  -9.34519594   7.77891522 -14.43639732  13.84193186]\n",
      " [ -1.90710347 -18.89960509 -14.88848068  13.79188831  -7.17898173]]\n",
      "[[4 0 2 1 3]\n",
      " [3 2 4 1 0]\n",
      " [3 1 2 0 4]\n",
      " [1 2 4 0 3]]\n"
     ]
    }
   ],
   "source": [
    "# argsort排序，返回下标\n",
    "data2 = np.random.uniform(-20, 20, size=(4, 5))\n",
    "print(data2)\n",
    "print(np.argsort(data2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "86354142-114c-4dee-ba1d-b82b91ae04c1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T10:39:41.410440Z",
     "iopub.status.busy": "2022-04-07T10:39:41.410190Z",
     "iopub.status.idle": "2022-04-07T10:39:41.415229Z",
     "shell.execute_reply": "2022-04-07T10:39:41.414580Z",
     "shell.execute_reply.started": "2022-04-07T10:39:41.410414Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-10.7030199    6.92446262  -4.30791428   8.44456621 -12.65383058]\n",
      " [ 15.89937381  15.24068584 -10.11422686 -14.3599904   11.00322691]\n",
      " [ 10.98773486  -9.34519594   7.77891522 -14.43639732  13.84193186]\n",
      " [ -1.90710347 -18.89960509 -14.88848068  13.79188831  -7.17898173]]\n",
      "======================================================================\n",
      "[[  8.44456621   6.92446262  -4.30791428 -10.7030199  -12.65383058]\n",
      " [ 15.89937381  15.24068584  11.00322691 -10.11422686 -14.3599904 ]\n",
      " [ 13.84193186  10.98773486   7.77891522  -9.34519594 -14.43639732]\n",
      " [ 13.79188831  -1.90710347  -7.17898173 -14.88848068 -18.89960509]]\n"
     ]
    }
   ],
   "source": [
    "# 降序排序\n",
    "print(data2)\n",
    "print(\"=\" * 70)\n",
    "print(-np.sort(-data2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "fa4fedec-157e-4533-b1bf-394f792d5592",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-07T10:52:58.342369Z",
     "iopub.status.busy": "2022-04-07T10:52:58.342117Z",
     "iopub.status.idle": "2022-04-07T10:52:58.348269Z",
     "shell.execute_reply": "2022-04-07T10:52:58.347546Z",
     "shell.execute_reply.started": "2022-04-07T10:52:58.342341Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-10.7030199    6.92446262  -4.30791428   8.44456621 -12.65383058]\n",
      " [ 15.89937381  15.24068584 -10.11422686 -14.3599904   11.00322691]\n",
      " [ 10.98773486  -9.34519594   7.77891522 -14.43639732  13.84193186]\n",
      " [ -1.90710347 -18.89960509 -14.88848068  13.79188831  -7.17898173]]\n",
      "======================================================================\n",
      "[[  8.44456621   6.92446262  -4.30791428 -10.7030199  -12.65383058]\n",
      " [-10.7030199    6.92446262 -12.65383058  -4.30791428   8.44456621]\n",
      " [-12.65383058 -10.7030199   -4.30791428   6.92446262   8.44456621]\n",
      " [  8.44456621 -10.7030199  -12.65383058  -4.30791428   6.92446262]]\n"
     ]
    }
   ],
   "source": [
    "# 降序排序2\n",
    "print(data2)\n",
    "print(\"=\" * 70)\n",
    "index = np.argsort(-data2)\n",
    "print(np.take(data2, index))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90a45dad-7f6c-4cee-8816-a55e14268ee7",
   "metadata": {},
   "source": [
    "## 其他函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "9f4647a9-91a4-45d2-95f7-f72da15d7858",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T02:47:13.746653Z",
     "iopub.status.busy": "2022-04-08T02:47:13.746335Z",
     "iopub.status.idle": "2022-04-08T02:47:13.753206Z",
     "shell.execute_reply": "2022-04-08T02:47:13.752523Z",
     "shell.execute_reply.started": "2022-04-08T02:47:13.746607Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-10.7030199    6.92446262  -4.30791428   8.44456621 -12.65383058]\n",
      "==============================\n",
      "[ 15.89937381  15.24068584 -10.11422686 -14.3599904   11.00322691]\n",
      "==============================\n",
      "[ 10.98773486  -9.34519594   7.77891522 -14.43639732  13.84193186]\n",
      "==============================\n",
      "[ -1.90710347 -18.89960509 -14.88848068  13.79188831  -7.17898173]\n",
      "==============================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([None, None, None, None], dtype=object)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试一下apply_along_axis\n",
    "def calcMean(x):\n",
    "    print(x)\n",
    "    print(\"=\" * 30)\n",
    "\n",
    "\n",
    "np.apply_along_axis(calcMean, axis=1, arr=data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "b973c62d-33c6-48c4-85ab-5dfd43b15a92",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T02:47:44.598696Z",
     "iopub.status.busy": "2022-04-08T02:47:44.598449Z",
     "iopub.status.idle": "2022-04-08T02:47:44.606855Z",
     "shell.execute_reply": "2022-04-08T02:47:44.605892Z",
     "shell.execute_reply.started": "2022-04-08T02:47:44.598669Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-10.7030199    6.92446262  -4.30791428   8.44456621 -12.65383058]\n",
      "-2.695490521407637\n",
      "[ 15.89937381  15.24068584 -10.11422686 -14.3599904   11.00322691]\n",
      "5.376561964771621\n",
      "[ 10.98773486  -9.34519594   7.77891522 -14.43639732  13.84193186]\n",
      "3.140484712978569\n",
      "[ -1.90710347 -18.89960509 -14.88848068  13.79188831  -7.17898173]\n",
      "-7.991521960707387\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([None, None, None, None], dtype=object)"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求一维数组去掉最大值和最小值之后的均值\n",
    "def calcMean(row):\n",
    "    print(row)\n",
    "    newRow = row[np.logical_and(row != row.max(), row != row.min())]\n",
    "    rowMean = newRow.mean()\n",
    "    print(rowMean)\n",
    "\n",
    "\n",
    "np.apply_along_axis(calcMean, axis=1, arr=data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "45a3dc78-7fc5-4e0e-a561-65ae2832a137",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T02:54:02.026268Z",
     "iopub.status.busy": "2022-04-08T02:54:02.025752Z",
     "iopub.status.idle": "2022-04-08T02:54:02.032550Z",
     "shell.execute_reply": "2022-04-08T02:54:02.031836Z",
     "shell.execute_reply.started": "2022-04-08T02:54:02.026240Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2.69549052,  5.37656196,  3.14048471, -7.99152196])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求一维数组去掉最大值和最小值之后的均值\n",
    "def calcMean(row):\n",
    "    # print(row)\n",
    "    newRow = row[np.logical_and(row != row.max(), row != row.min())]\n",
    "    rowMean = newRow.mean()\n",
    "    # print(rowMean)\n",
    "    return rowMean\n",
    "\n",
    "\n",
    "# 求data2中每行去掉最大值和最小值之后的均值\n",
    "np.apply_along_axis(calcMean, axis=1, arr=data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "8840af0b-73c0-43df-8714-66afd8878afb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T03:28:23.777075Z",
     "iopub.status.busy": "2022-04-08T03:28:23.776821Z",
     "iopub.status.idle": "2022-04-08T03:28:23.783494Z",
     "shell.execute_reply": "2022-04-08T03:28:23.782786Z",
     "shell.execute_reply.started": "2022-04-08T03:28:23.777048Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2.69549052,  5.37656196,  3.14048471, -7.99152196])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求data2中每行去掉最大值和最小值之后的均值\n",
    "np.apply_along_axis(\n",
    "    lambda x: x[np.logical_and(x != x.max(), x != x.min())].mean(), axis=1, arr=data2\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "74ed4d36-3242-427b-b1e7-7d4d10e2d9a0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T03:38:25.821743Z",
     "iopub.status.busy": "2022-04-08T03:38:25.821435Z",
     "iopub.status.idle": "2022-04-08T03:38:25.827032Z",
     "shell.execute_reply": "2022-04-08T03:38:25.826203Z",
     "shell.execute_reply.started": "2022-04-08T03:38:25.821716Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  1.11111111,  2.22222222,  3.33333333,  4.44444444,\n",
       "        5.55555556,  6.66666667,  7.77777778,  8.88888889, 10.        ])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0-10之间分成10等份\n",
    "np.linspace(0, 10, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "61bb6d3e-241c-47f5-97a6-f770c0601bc8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T03:38:33.748993Z",
     "iopub.status.busy": "2022-04-08T03:38:33.748746Z",
     "iopub.status.idle": "2022-04-08T03:38:33.754081Z",
     "shell.execute_reply": "2022-04-08T03:38:33.753367Z",
     "shell.execute_reply.started": "2022-04-08T03:38:33.748967Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1-10之间分成10等份\n",
    "np.linspace(1, 10, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "b855f904-18d9-4096-b3f4-b69e86f34bdf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T03:43:09.000997Z",
     "iopub.status.busy": "2022-04-08T03:43:09.000746Z",
     "iopub.status.idle": "2022-04-08T03:43:09.006583Z",
     "shell.execute_reply": "2022-04-08T03:43:09.005742Z",
     "shell.execute_reply.started": "2022-04-08T03:43:09.000970Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 0.11111111, 0.22222222, 0.33333333, 0.44444444,\n",
       "       0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0-1之间分成10等份\n",
    "np.linspace(0, 1, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "7842b3df-3afd-4b4b-8586-9b3ba98f1b25",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T03:48:31.620852Z",
     "iopub.status.busy": "2022-04-08T03:48:31.620569Z",
     "iopub.status.idle": "2022-04-08T03:48:31.626282Z",
     "shell.execute_reply": "2022-04-08T03:48:31.625652Z",
     "shell.execute_reply.started": "2022-04-08T03:48:31.620824Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2, 4, 4, 4],\n",
       "       [3, 1, 2, 1, 3],\n",
       "       [1, 2, 4, 3, 3]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data3 = np.random.randint(1,5,size=(3,5))\n",
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "a3502348-7604-43ea-baf0-3891846fb832",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T03:50:43.887995Z",
     "iopub.status.busy": "2022-04-08T03:50:43.887618Z",
     "iopub.status.idle": "2022-04-08T03:50:43.893323Z",
     "shell.execute_reply": "2022-04-08T03:50:43.892535Z",
     "shell.execute_reply.started": "2022-04-08T03:50:43.887967Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回data3中的唯一值\n",
    "np.unique(data3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "c6f32bbb-1420-4e32-b71e-e2886b468047",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-08T03:54:44.914616Z",
     "iopub.status.busy": "2022-04-08T03:54:44.914361Z",
     "iopub.status.idle": "2022-04-08T03:54:44.919984Z",
     "shell.execute_reply": "2022-04-08T03:54:44.919348Z",
     "shell.execute_reply.started": "2022-04-08T03:54:44.914587Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 2, 3, 4]), array([3, 3, 4, 5]))"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回data3中的唯一值，统计每个值出现的次数\n",
    "np.unique(data3, return_counts=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "532b1ecf-bfe9-4078-8fe2-3167d6d9c56a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
